#include "sys.h"
#include "log.h"
#include "capture.h"
#include "thread_pool.h"

static char ifName[32] = "";
static int ifPromisc = 0;
static OPT_T opts[] = {
    { "-i", ifName, OPT_STRING, "bind to interface." },
    { "-p", &ifPromisc, OPT_NONE, "set interface in promisc mode." },
};

extern int pcap_output_init(int argc, char** argv);

extern int dns_hook(Packet_t* packet);
extern int icmp_hook(Packet_t* packet);
extern int all_hook(Packet_t* packet);
extern int pcap_output_hook(Packet_t* packet);

int regist_hooks()
{
    hook_register(NULL, dns_hook);
    hook_register(NULL, icmp_hook);
    hook_register(NULL, all_hook);
    hook_register(pcap_output_init, pcap_output_hook);

    return 1;
}

int capture_main(int argc, char** argv)
{

    opt_parse(opts, sizeof(opts), argc, argv);

    regist_hooks();
    Hooks_init(argc, argv);

    Capture_t* cap = capture_new();
    if (strlen(ifName) > 0) {
        capture_bind(cap, ifName);
        if (ifPromisc) {
            capture_set_promisc(cap, ifName);
        }
    }
    capture_run(cap);
    capture_free(cap);
    return 0;
}
/*void *thread_handle(void *arg){
    log_debug("tid:%u, i:%d", pthread_self(), (int)arg);
    usleep(500 * 1000);
    return NULL;
}
int thread_pool_main(int argc, char **argv){
    thread_pool_t *tp = thread_pool_new(10);
    thread_pool_set_auto_extern(tp);
    int i =0;
    for(i =0; i < 100; i++){
        thread_pool_add_task(tp, thread_handle, (void *)i);
    }

    while(1){
        sleep(1);
        int waiting_task_number = thread_pool_get_task_number(tp);
        log_debug("running number:%d", thread_pool_get_running_number(tp));
        log_debug("task number:%d", waiting_task_number);
        
    }
}*/
int main(int argc, char **argv){
    log_initv(argc, argv);
    capture_main(argc, argv);
    // thread_pool_main(argc, argv);
    return 0;
}